package servlet;

import com.alibaba.fastjson.JSON;
import pojo.AccessControlDevice;
import pojo.Building;
import service.AccessControlDeviceService;
import service.BuildingService;
import service.impl.AccessControlDeviceServiceImpl;
import service.impl.BuildingServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@WebServlet(urlPatterns = "/building")
public class BuildingServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求的字符编码
        request.setCharacterEncoding("UTF-8");

        //设置响应的字符编码
        response.setCharacterEncoding("UTF-8");

        //设置内容
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        BuildingService buildingService = new BuildingServiceImpl();
        List<Building> list = buildingService.list();
        String jsonString = JSON.toJSONString(list);
        out.print(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
